from django.db import models
from django.contrib.auth.models import AbstractUser
from db.base_model import BaseModel

# Create your models here.
class User(AbstractUser, BaseModel):
    '''用户类'''
    class Meta:
        db_table = 'df_user'
        verbose_name = '用户'
        verbose_name_plural = verbose_name # 取消模型名称的复数形式


class ManageAddress(models.Manager):
    '''地址模型管理器类'''
    # 封装用户对地址模型操作的方法
    def get_default_address(self, user):
        try:
            default_address = self.get(user=user.id, is_default=True)
        except:
            default_address = None
        return default_address


class Address(BaseModel):
    '''地址类'''
    user = models.ForeignKey('User', on_delete=models.CASCADE, verbose_name="所属账户")
    receiver = models.CharField(max_length=20, default='', verbose_name="收件人")
    addr = models.CharField(max_length=256, default='', verbose_name="收件地址")
    zip_code = models.CharField(max_length=6, default='',verbose_name="邮编")
    phone = models.CharField(max_length=11, default='', verbose_name="联系方式")
    is_default = models.BooleanField(default=False, verbose_name="是否默认")

    objects = ManageAddress()

    class Meta:
        db_table = 'df_address'
        verbose_name = '地址'
        verbose_name_plural = verbose_name